

import React, { createContext, useState } from 'react'
import classNames from 'classnames'
import { MenuItemProps } from './menuItem';

type MenuMode = 'horizontal' | 'vertical'
type SelectCallback = (selectedIndex: number) => void

export interface MenuProps {
  defaultIndex?: number;
  className?: string;
  mode?: MenuMode;
  style?: React.CSSProperties;
  onSelect?: (selectedIndex: number) => void
}


interface IMenuContext {
  index: number,
  onSelect?: SelectCallback,
  mode?: MenuMode;
}

export const MenuContext = createContext<IMenuContext>({ index: 0 })


const Menu: React.FC<MenuProps> = (props) => {
  const { className, mode, style, children, defaultIndex, onSelect } = props
  const [currentActive, setActive] = useState(defaultIndex)
  const classes = classNames('zh-menu', className, {
    'menu-vertical': mode === 'vertical',
    'menu-horizontal': mode !== 'vertical',

  })

  const handlelick = (index: number) => {
    setActive(index)
    if (onSelect) {
      onSelect(index)
    }
  }
  const passedContext: IMenuContext = {
    index: currentActive ? currentActive : 0,
    onSelect: handlelick,
    mode: mode
  }

  const renderChildren = () => {
    return React.Children.map(children, (child, index)=>{
      const childElement = child as React.FunctionComponentElement<MenuItemProps>
      const {displayName} = childElement.type 
     if(displayName === 'MenuItem' || displayName === 'subMenu'){
       return React.cloneElement(childElement,{
         index
       })
     }else {
       console.log("warning: menu has a child chich is not a menuitem")
     }
    })
  }

  return (
    <ul className={classes} style={style} data-testid="test-menu">
      <MenuContext.Provider value={passedContext}>
        {renderChildren()}
      </MenuContext.Provider>
    </ul>
  )

}





Menu.defaultProps = {
  defaultIndex: 0,
  mode: 'vertical',
}

export default Menu